package com.partsoft.dits.integrate;

import java.util.Date;

import org.springframework.util.ClassUtils;

import com.partsoft.dits.AbstractDataEdgeDescriptor;
import com.partsoft.dits.DataChangeLog;
import com.partsoft.dits.DataEdgeDescriptor;
import com.partsoft.dits.message.DataAction;

public class IdNumericDataChangeLog extends AbstractDataEdgeDescriptor implements DataChangeLog {

	private long id;

	private String descriptorClassName;
	
	private String descriptorIdentity;
	
	private String dataIdentity;
	
	private Date recordedTime;
	
	private DataAction dataAction;
	
	private String namespace;

	private Class<? extends DataEdgeDescriptor> descriptorClazz = null;

	public String getId() {
		return Long.valueOf(getRealId()).toString();
	}
	
	
	public long getRealId() {
		return id;
	}
	
	public void setRealId(long id) {
		this.id = id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public void setDescriptorClassName(String descriptorClassName) {
		this.descriptorClassName = descriptorClassName;
		this.descriptorClazz = null;
	}

	public String getDescriptorClassName() {
		return descriptorClassName;
	}

	@SuppressWarnings("unchecked")
	public Class<? extends DataEdgeDescriptor> getDescriptorClass() {
		Class<? extends DataEdgeDescriptor> result = this.descriptorClazz;
		if (this.descriptorClazz == null) {
			try {
				this.descriptorClazz = (Class<? extends DataEdgeDescriptor>) ClassUtils.forName(
						getDescriptorClassName(), Thread.currentThread().getContextClassLoader());
				result = this.descriptorClazz;
			} catch (Throwable e) {
				throw new IllegalArgumentException(e.getMessage(), e);
			}
		}
		return result;
	}

	public void setDescriptorClass(Class<? extends DataEdgeDescriptor> clazz) {
		this.descriptorClazz = clazz;
		if (clazz != null) {
			this.descriptorClassName = ClassUtils.getUserClass(clazz).getName();
		} else {
			this.descriptorClassName = null;
		}
	}


	public String getDescriptorIdentity() {
		return descriptorIdentity;
	}


	public void setDescriptorIdentity(String descriptorIdentity) {
		this.descriptorIdentity = descriptorIdentity;
	}


	public String getDataIdentity() {
		return dataIdentity;
	}


	public void setDataIdentity(String dataIdentity) {
		this.dataIdentity = dataIdentity;
	}


	public Date getRecordedTime() {
		return recordedTime;
	}


	public void setRecordedTime(Date recordedTime) {
		this.recordedTime = recordedTime;
	}


	public Class<? extends DataEdgeDescriptor> getDescriptorClazz() {
		return descriptorClazz;
	}

	public void setDescriptorClazz(Class<? extends DataEdgeDescriptor> descriptorClazz) {
		this.descriptorClazz = descriptorClazz;
	}


	public String getNamespace() {
		return namespace == null ? "" : namespace;
	}

	public void setNamespace(String namespace) {
		this.namespace = namespace;
	}


	public DataAction getDataAction() {
		return dataAction;
	}


	public void setDataAction(DataAction dataAction) {
		this.dataAction = dataAction;
	}
	
}
